 using System;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AddressableAssets;
 using UnityEngine.ResourceManagement.AsyncOperations;
 using Random = UnityEngine.Random;

 public class CardManager : MonoBehaviour
{
    public PoolTool poolTool;
    public GameLevel gameLevel;

    //游戏中所有可能出现的卡牌
    public CardDataListSO cardDataListSO;
    public List<CardDataDetails> cardPrimaryDataList;
    public List<CardDataDetails> cardIntermeiateDataList;
    public List<CardDataDetails> cardSeniorDataList;

    [Header("初始卡牌库")] public List<CardLibraryEntry>  newGameLibrary;
    [Header("玩家卡牌库")] public List<CardLibraryEntry> currentLibrary;

    private int previousIndex;

    private void Start()
    {
        InitializeCardDataList();
        //初始化玩家卡牌库
        foreach (var item in newGameLibrary)
        {
            currentLibrary.Add(item);
        }
    }

    private void OnDisable()
    {
        currentLibrary.Clear();
    }

    //获得项目中所有的卡牌数据
    private void InitializeCardDataList()
    {
        cardPrimaryDataList = new();
        cardIntermeiateDataList = new();
        cardSeniorDataList = new();
        
        foreach (var card in cardDataListSO.cardDataList)
        {
            if (card.isPrimaryCard)
            {
                cardPrimaryDataList.Add(card);
            }
        }

        foreach (var card in cardDataListSO.cardDataList)
        {
            if(card.isPrimaryCard || card.isIntermediateCard) 
            {
                cardIntermeiateDataList.Add(card);
            }
        }

        foreach (var card in cardDataListSO.cardDataList)
        {
            cardSeniorDataList.Add(card);
        }
        
    }

    public void OnInitCardList()
    {
        if (gameLevel == GameLevel.Primary)
        {
            InitializeNewGameCardDataList(cardPrimaryDataList);
        }
        else if (gameLevel == GameLevel.Intermdiate)
        {
            InitializeNewGameCardDataList(cardIntermeiateDataList);
        }
        else if (gameLevel == GameLevel.Senior)
        {
            InitializeNewGameCardDataList(cardSeniorDataList);
        }
    }
    
    private void InitializeNewGameCardDataList(List<CardDataDetails> cardDataList)
    {
        newGameLibrary = new();
        
        foreach (var card in cardDataList)
        {
            if (card.cardName == "王国之剑")
            {
                newGameLibrary.Add(new CardLibraryEntry
                {
                    cardData = card,
                    amount = 5
                });
            }
            else if (card.cardName == "王国布甲")
            {
                newGameLibrary.Add(new CardLibraryEntry
                {
                    cardData = card,
                    amount = 4
                });
            }
            else if (card.cardName == "苹果" || card.cardName == "来源之书")
            {
                newGameLibrary.Add(new CardLibraryEntry
                {
                    cardData = card,
                    amount = 2
                });
            }
            else
            {
                newGameLibrary.Add(new CardLibraryEntry
                {
                    cardData = card,
                    amount = 1
                });
            }
        }
    }

    public GameObject GetCardObject()
    {
        var carObject = poolTool.GetObjectFromPool();
        carObject.transform.localScale = Vector3.zero;
        return carObject;
    }
    
    public void DiscardCardObject(GameObject obj)
    {
        poolTool.ReturnObjectToPool(obj);
    }

    public CardDataDetails GetNewCardData()
    {
        var randomIndex = 0;
        do
        {
            if (gameLevel == GameLevel.Primary)
                randomIndex = Random.Range(0, cardPrimaryDataList.Count);
            else if (gameLevel == GameLevel.Primary)
                randomIndex = Random.Range(0, cardIntermeiateDataList.Count);
            else
                randomIndex = Random.Range(0, cardSeniorDataList.Count);
            
        } while (previousIndex == randomIndex);

        previousIndex = randomIndex;
        if (gameLevel == GameLevel.Primary)
            return cardPrimaryDataList[previousIndex];
        else if (gameLevel == GameLevel.Primary)
            return cardIntermeiateDataList[previousIndex];
        else
            return cardSeniorDataList[previousIndex];
    }

    /// <summary>
    /// 解锁添加卡槽
    /// </summary>
    /// <param name="newCardData"></param>
    public void UnlockCard(CardDataDetails newCardData)
    {
        var newCard = new CardLibraryEntry
        {
            cardData = newCardData,
            amount = 1
        };
        if (currentLibrary.Contains(newCard))
        {
            var target = currentLibrary.Find(x => x.cardData == newCardData);
            target.amount++;
        }
        else
        {
            currentLibrary.Add(newCard);
        }
    }
    
    public void OnPrimaryButtonEvent()
    {
        gameLevel = GameLevel.Primary;
    }
    public void OnIntermediateButtonEvent()
    {
        gameLevel = GameLevel.Intermdiate;
    }
    public void OnSeniorButtonEvent()
    {
        gameLevel = GameLevel.Senior;
    }
}
